package impl;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;

import baise.BaseDaoImpl;
import baise.iHibBaseDAO;
import baise.iHibBaseDAOimpl;
import model.TBook;
import model.TLocation;
import DAO.location;

public class locationimpl implements location {
	iHibBaseDAO iBaseDAO=new iHibBaseDAOimpl();
	@Override
	public int floor(TLocation tl) {
		BaseDaoImpl bea=new BaseDaoImpl();
		String sql="insert into T_location(locationid,bookid,name) values(?,?,?)";
		Object[] para = {tl.getLocationid(),tl.getBookid(),tl.getName()};		
		return bea.insert(sql,para);
	}
	

	@Override
	public List<TLocation> seleLocation(int bookid) {
		String hql="select * from T_Location where bookid="+bookid;
		BaseDaoImpl bea=new BaseDaoImpl();
		ResultSet rs=bea.select(hql);
		
		List<TLocation> tl=null;
		try {
			tl = convertList(rs);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return tl;
	}
	
	private static List convertList(ResultSet rs) throws SQLException{
		List list = new ArrayList();
		ResultSetMetaData md = rs.getMetaData();//��ȡ����
		int columnCount = md.getColumnCount();//��ȡ�е�����
		while (rs.next()) {
			Map rowData = new HashMap();//����Map
				for (int i = 1; i <= columnCount; i++) {
					rowData.put(md.getColumnName(i), rs.getObject(i));//��ȡ����ֵ
				}
				list.add(rowData);
			}
		return list;
	}
	@Override
	public String update(TLocation tl) {
		//String hql="update TLocation where locationid = "+tl.getLocationid();
		if (iBaseDAO.update(tl)) {
			return "1";
		}
		return null;
	}
	@Override
	public boolean delect(TLocation tl) {
		if (iBaseDAO.delete(tl)) {
			String hql="delete from T_Location where bookid="+tl.getLocationid();
			iBaseDAO.delete(hql);
			return true;
		}
		return false;
	}
	@Override
	public Object addlocation(TLocation tl) {
		
		return iBaseDAO.insert(tl);
	}
//	public static void main(String[] args) {
//		TLocation t =new TLocation();
//		t.setLocationid(198);
//		t.setBookid(0);
//		t.setName("8F");
//		location l=new locationimpl();
////		;
//		System.out.println(l.floor(t));
////		//int st=(Integer)l.addlocation(t);
////		//boolean list= l.delect(t);  
////		List<TLocation> list= l.seleLocation(0);
////		String json=JSON.toJSONString(list);
////		System.out.println(json);
//	}
}
